package cn.dylan.user.controller;

import cn.dylan.basic.exception.BusinessException;
import cn.dylan.basic.util.JsonResult;
import cn.dylan.user.dto.LoginDto;
import cn.dylan.user.dto.WxDto;
import cn.dylan.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping
    @RequestMapping("/account")
    public JsonResult handelerLogin(@RequestBody LoginDto dto){
        try {
            return loginService.accountLogin(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙，请稍后重试");
        }
    }


    @PostMapping("/wechat/{code}")
    public JsonResult wechatLogin(@PathVariable String code){
        try {
            return loginService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙，请稍后重试");
        }
    }
    /**
     * 微信绑定
     */
    @PostMapping("/wechat/binder")
    public JsonResult weChatBind(@RequestBody WxDto dto){
        try {

            return loginService.binder(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙，请稍后重试");
        }
    }

}
